package cn.green.core.components.store.field;

import cn.green.core.components.exceptions.FieldFormNullException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.green.core.components.field.FieldFormTypeCom;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className FieldFormTypeStore
 * @description 属性格式仓库
 * @date 2024/11/15 15:14
 */
public class FieldFormTypeStore {

    private static final Map<String, FieldFormTypeCom> FIELD_PATTERN_MAP = new HashMap<>();
    private static final JSONArray FIELD_PATTERN_OPTIONS = new JSONArray();

    public static void put(FieldFormTypeCom pattern) {
        FIELD_PATTERN_MAP.put(pattern.patternCode(), pattern);
        JSONObject option = new JSONObject();
        option.put("label", pattern.patternName());
        option.put("value", pattern.patternCode());
        FIELD_PATTERN_OPTIONS.add(option);
    }

    public static FieldFormTypeCom get(String fieldPatternCode) {
        if (!FIELD_PATTERN_MAP.containsKey(fieldPatternCode)) {
            try {
                throw new FieldFormNullException(fieldPatternCode);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

        }
        return FIELD_PATTERN_MAP.get(fieldPatternCode);
    }

    public static JSONArray options() {
        return FIELD_PATTERN_OPTIONS;
    }
}
